/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
 */

package com.huawei.fastble.adapter;
/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.huawei.fastble.ResourceTable;
import com.huawei.fastble.bean.BluetoothDevice;
import com.huawei.fastble.slice.MainAbilitySlice;

import com.clj.fastble.data.BleDevice;

import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.bluetooth.BluetoothRemoteDevice;

import java.util.List;

/**
 * BleItemProvider extends BaseItemProvider
 *
 */
public class BleItemProvider extends BaseItemProvider {
    private MainAbilitySlice bluetoothAbilitySlice;

    private List<BleDevice> bluetoothDeviceList;

    public BleItemProvider(MainAbilitySlice bluetoothAbilitySlice, List<BleDevice> itemList) {
        this.bluetoothAbilitySlice = bluetoothAbilitySlice;
        bluetoothDeviceList = itemList;
    }

    private ConnectOnclickListener listener;
    private HandleOnclickListener handleOnclickListener;

    public void setListener(ConnectOnclickListener listener) {
        this.listener = listener;
    }

    public void setHandleOnclickListener(HandleOnclickListener handleOnclickListener) {
        this.handleOnclickListener = handleOnclickListener;
    }

    @Override
    public int getCount() {
        return bluetoothDeviceList.size();
    }

    @Override
    public Object getItem(int position) {
        return bluetoothDeviceList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        return getRootView(position);
    }

    private Component getRootView(int position) {
        Text deviceName = null;

        Component rootView =
                LayoutScatter.getInstance(bluetoothAbilitySlice).parse(ResourceTable.Layout_ble_list_item, null, false);

        Component itemComponent = rootView.findComponentById(ResourceTable.Id_bluetooth_device_name);
        if (itemComponent instanceof Text) {
            deviceName = (Text) itemComponent;
        }

        BleDevice bleDevice = bluetoothDeviceList.get(position);
        if (deviceName != null) {
            deviceName.setText("Rssi:" + bleDevice.getRssi());
        }

        Text txt_man = (Text) rootView.findComponentById(ResourceTable.Id_txt_man);
        // 制造商
        if (bleDevice.getBleDevice() == null) {
            txt_man.setText("制造商数据：居然是空");
        } else {
            txt_man.setText("制造商数据：" + bleDevice.getManufacturer());
        }

        Button button = (Button) rootView.findComponentById(ResourceTable.Id_btn_start_pair);
        if (bleDevice.isConnectable()) {
            button.setVisibility(Component.VISIBLE);
        } else {
            button.setVisibility(Component.HIDE);
        }
        if (bleDevice.getConnectState().equals("已连接")) {
            button.setTextColor(Color.BLUE);

        } else if (bleDevice.getConnectState().equals("连接失败") || bleDevice.getConnectState().equals("连接断开")) {
            button.setTextColor(Color.RED);
        } else {
            button.setTextColor(Color.WHITE);
        }

        button.setText(bleDevice.getConnectState());

        button.setClickedListener(
                component -> {
                    listener.click(bleDevice);
                });

        Button btnHandle = (Button) rootView.findComponentById(ResourceTable.Id_btn_handle);
        if (bleDevice.getConnectState().equals("已连接")) {
            btnHandle.setVisibility(Component.VISIBLE);

        } else {
            btnHandle.setVisibility(Component.HIDE);
        }
        btnHandle.setClickedListener(
                component -> {
                    handleOnclickListener.click(bleDevice);
                });

        return rootView;
    }

    /**
     * updates available Bluetooth devices in UI
     *
     * @param devices list of Bluetooth devices
     */
    public void updateDeviceList(List<BleDevice> devices) {
        bluetoothDeviceList.clear();
        bluetoothDeviceList.addAll(devices);
        notifyDataChanged();
    }

    /**
     * addDevice
     * @param bleDevice bleDevice
     */
    public void addDevice(BleDevice bleDevice) {
        bluetoothDeviceList.add(bleDevice);
        notifyDataChanged();
    }

    /**
     * removeDevice
     * @param bleDevice bleDevice
     */
    public void removeDevice(BleDevice bleDevice) {
        for (int i = 0; i < bluetoothDeviceList.size(); i++) {
            BleDevice device = bluetoothDeviceList.get(i);
            if (bleDevice.getKey().equals(device.getKey())) {
                bluetoothDeviceList.remove(i);
            }
        }
    }

    /**
     * ConnectOnclickListener
     */
    public interface ConnectOnclickListener {
        /**
         * click
         * @param bleDevice bleDevice
         */
        void click(BleDevice bleDevice);
    }

    /**
     * HandleOnclickListener
     */
    public interface HandleOnclickListener {
        /**
         * click
         * @param bleDevice bleDevice
         */
        void click(BleDevice bleDevice);
    }
}
